﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProcessAnalysis
{
    public partial class RunningOptionsTool : Form
    {
        public bool saveFlag;
        private RunningOptionsProperties prevOptions;
        private RunningOptionsProperties currentOptions;

        public RunningOptionsTool(RunningOptionsProperties options)
        {
            currentOptions = options;
            prevOptions = new RunningOptionsProperties(options);
            InitializeComponent();
            AddProperties(options);
        }

        private void AddProperties(RunningOptionsProperties options)
        {
            propertyGrid.SelectedObject = options;
        }

        private void OkBtnClick(object sender, EventArgs e)
        {
            saveFlag = true;
            Close();
        }

        private void CancelBtnClick(object sender, EventArgs e)
        {
            Close();
        }

        private void RunningOptionsToolFormClosing(object sender, FormClosingEventArgs e)
        {
            if (!saveFlag)
            {
                currentOptions.Init(prevOptions);
            }
        }
    }

    public class RunningOptionsProperties
    {
        public RunningOptionsProperties() 
        {
            ProcessesRunningTime = 15;
            foreach (var property in typeof(RunningOptionsProperties).GetProperties())
            {
                if (property.PropertyType != ProcessesRunningTime.GetType())
                {
                    property.SetValue(this, false, null);
                }
            }
        }

        public RunningOptionsProperties(RunningOptionsProperties optionsObject)
        {
            Init(optionsObject);
        }

        public void Init(RunningOptionsProperties optionsObject)
        {
            ProcessesRunningTime = optionsObject.ProcessesRunningTime;

            CpuConsumptionComparison = optionsObject.CpuConsumptionComparison;
            PrivateMemoryUse = optionsObject.PrivateMemoryUse;
            PagingSpace = optionsObject.PagingSpace;
            NumberOfThread = optionsObject.NumberOfThread;
            VirtualMemorySize = optionsObject.VirtualMemorySize;
            PhisicalMemory = optionsObject.PhisicalMemory;
            ModulesLoaded = optionsObject.ModulesLoaded;
            TrafficReceived = optionsObject.TrafficReceived;
            TrafficSent = optionsObject.TrafficSent;
            CompressionRatio = optionsObject.CompressionRatio;
            DeCompressionRatio = optionsObject.DeCompressionRatio;
        }

        public bool IsSomeGraphChoosen()
        {
            foreach (var property in typeof(RunningOptionsProperties).GetProperties())
            {
                if (property.GetValue(this, null).ToString() == "True")
                {
                    return true;
                }
            }
            return false;
        }

        [CategoryAttribute("Running Configurations"), DisplayName("Processes Running Time"), 
            DescriptionAttribute("The program's running time in seconds")]
        public int ProcessesRunningTime { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Cpu Consumption Comparison")]
        public bool CpuConsumptionComparison { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Private Memory Use")]
        public bool PrivateMemoryUse { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Paging Space")]
        public bool PagingSpace { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Number Of Thread")]
        public bool NumberOfThread { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Virtual Memory Size")]
        public bool VirtualMemorySize { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Phisical Memory")]
        public bool PhisicalMemory { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Modules Loaded")]
        public bool ModulesLoaded { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Traffic Received")]
        public bool TrafficReceived { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Traffic Sent")]
        public bool TrafficSent { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("Compression Ratio")]
        public bool CompressionRatio { get; set; }

        [CategoryAttribute("Graphs Displays Configurations"), DisplayName("DeCompression Ratio")]
        public bool DeCompressionRatio { get; set; }
    }
}

